# encoding: utf-8

module Invite
    def go_notice_invite
        go_notice
        
        # 进入邀请通知
        @driver.find_element(:xpath, "(//a[contains(@href, '/invitation_notifications/received')])[2]").click
    end

    def go_notice_star
        go_notice

        #进入关注通知
        @driver.find_element(:xpath, "//ul[@class='sidebar_nav']/li[3]/a").click

    end

    # 进入收到的邀请
    def go_receive_invite
        go_notice_invite

        @driver.find_element(:css, "a.invi_notice_selected").click
    end

  # 邀请项目成员
  def invite(project_user, project, invite_user)
    project_username = (project_user.class == Hash ? project_user['username']: project_user)
    project_name = (project.class == Hash ? project['project_name']: project)
    go_member(project_username, project_name)

    invite_username = invite_user['nickname']+'('+invite_user['username']+')'
    jq = <<JQ
    $("#autocomplete").val("#{invite_username}");
    $("#btn_invitation").prop("disabled", false);
JQ
    @driver.execute_script jq
    @driver.find_element(:id, "btn_invitation").click
  end

  # 接受邀请
  def accept_invite
    go_receive_invite
    @driver.find_element(:css, ".ico_correct").click
  end

  # 设置用户为观察者
  def set_observer(project_user, project)
    project_username = (project_user.class == Hash ? project_user['username']: project_user)
    project_name = (project.class == Hash ? project['project_name']: project)
    go_member(project_username, project_name)

    @driver.find_element(:xpath, "//option[@value=10]").click
  end

  # 在project中，设置user的identity
  # user 被邀请用户
  def set_user_identity(project_user, project, user, identity = 'default')
    project_username = get_hash(project_user, 'username')
    project_name = get_hash(project, 'project_name')
    user_nickname = get_hash(user, 'nickname')
    identity_value = (identity == 'default'? 10:30)

    go_member(project_username, project_name)

    tt = @driver.find_element(:xpath, "//table[@class = 'table table-hover project_setting_table']/tbody").find_elements(:tag_name, 'tr')
    tt.each do |t|
      if t.text.include? user_nickname then
        t.find_element(:css, "td:nth-child(2) select option[value='#{identity_value}']").click
      end
    end
  end
end
